<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Shop\Order as OrderResource;
use App\Http\Resources\Shop\OrderCollection;
use App\Http\Controllers\ApiController;
use App\Models\Order;
use Carbon\Carbon;
use Auth;

class OrderController extends ApiController
{
    /**
     * 订单列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(REQUEST $request)
    {
        $status = $request->input('status', '');
        $limit = $request->input('size', 15);
        $order_no = $request->input('order_no', 0);
        $user_name = $request->input('user_name', '');
        $date = $request->input('date');

        $orm = Order::where('shop_id', $request->user('shop')->id)->orderBy('id','DESC');

        $status && $orm->where('status', $status);
        $order_no && $orm->where('order_no', $order_no);

        if($user_name) {
            // 根据用户查找
            if (preg_match('/^1([0-9]{9})/', $user_name)) { 
                // 是手机号
                $userOrm = User::where('telphone', $user_name);
            }else{
                // 不是手机号根据昵称筛选
                $userOrm = User::where('name', 'like', '%'.$user_name.'%');
            }

            $user = $userOrm->first();

            if($user){
                $orm->where('user_id', $user->id);
            }else {
                $orm->where('user_id', -1);
            }
        }

        if($date) {
            if($date[0] && time($date[0])) {
                $orm->where('created_at', '>', $date[0]);
            }

            if($date[1] && time($date[1])) {
                $orm->where('created_at', '<', $date[1]);
            }
        }

        $orm->with(['user' => function($query) {
            $query->select('id', 'name', 'email', 'telphone', 'realname', 'address');
        }]);
        $orm->with('orderItems');

        return new OrderCollection($orm->paginate($limit));
    }

    /**
     * 订单详情
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $order = Order::ownerShop()
            ->where('id', $id)
            ->with(['user' => function($query) {
                $query->select(['id', 'name', 'email', 'telphone', 'realname', 'address']);
            }])
            ->with('orderItems')
            ->first();
        if ($order) {
            return $this->success(new OrderResource($order));
        } else {
            return $this->failed('获取不到订单');
        }
    }

    /**
     * 更改状态
     * 
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updateStatus(Request $request, $id)
    {
        $status = $request->input('status');

        if (Order::getOrderStatus($status)) {
            // TODO
        }
    }

    /**
     * 确认订单
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function confirm($id)
    {
        $order = Order::ownerShop()->where('id', $id)->first();

        if (!$order) {
            return $this->failed('订单不存在');
        }

        if ($order->status !== Order::STATUS_UNCONFIRMED) {
            return $this->failed('更新失败');
        }

        $order->status = Order::STATUS_UNPAID;
        $order->save();
        $order->confirm_at = Carbon::now();

        return $this->message('订单已经确认');
    }

    /**
     * 取消订单
     *
    * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function cancel($id)
    {
        $order = Order::ownerShop()->where('id', $id)->first();

        if (!$order) {
            return $this->failed('订单不存在');
        }

        if ($order->status !== Order::STATUS_UNPAID) {
            return $this->failed('订单不予许取消');
        }

        $order->status = Order::STATUS_CANCELLED;
        $order->cancel_at = Carbon::now();
        $order->save();

        return $this->message('订单已关闭');
    }

    /**
     * 订单发货
     *
     * @param [type] $id
     * @return void
     */
    public function deliver($id)
    {
        $order = Order::ownerShop()->where('id', $id)->first();

        if (!$order) {
            return $this->failed('订单不存在');
        }

        if ($order->status !== Order::STATUS_PAID) {
            return $this->failed('修改失败');
        }

        $order->status = Order::STATUS_DELIVERING;
        $order->delivery_at = Carbon::now();
        $order->save();

        return $this->message('订单状态更改为发货状态');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
